<?php

use Doraemon\cache\Content\ContentRelatedCategory as cContentCate;
use Doraemon\model\Content\ContentRelatedCategory as DbContentCate;
use Doraemon\tools\Log;

class ContentCateAction extends ActionPlugin
{
    const IS_TRACING = true;

    const _DATABASE_ = 'hhzhome_content';

    const _TABLE_NAME = 'content_related_category';

    const TYPE_ADD = "insert";

    const TYPE_UPDATE = "update";

    /**
     * @throws Exception
     */
    public function main()
    {
        Log::info('contentCate 获取数据', $this->messageBody);
        if ($this->messageBody['database'] != self::_DATABASE_) throw new \Exception('数据库错误');
        if ($this->messageBody['table'] != self::_TABLE_NAME) throw new \Exception('数据表错误');
        if (!in_array($this->messageBody['type'], [self::TYPE_UPDATE, self::TYPE_ADD])) throw new \Exception('不是新增操作');
        if (!$this->messageBody['data']) throw new \Exception('未接收到数据');
        $objId = $this->messageBody['data']['obj_id'] ?? 0;
        $dbData = DbContentCate::get('*', [
            'status' => DbContentCate::STATUS_NORMAL,
            'obj_id' => $objId,
        ]);
        if (!empty($dbData)) {
            cContentCate::setValue($objId, $dbData);
        }
        Log::info("updateContentCate", ['objId' => $objId, 'hasDbData' => !empty($dbData)]);
        $this->setSuccess();
    }
}